package pl.us.ij.plugins.segmentation.forms;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import pl.us.ij.plugins.segmentation.commands.edges.BranchCuttingCommand;
import pl.us.ij.plugins.segmentation.commands.edges.SkeletonCommand;
import pl.us.ij.plugins.segmentation.commands.edges.SkeletonCommandParams;
import pl.us.ij.plugins.segmentation.infrastructure.CategoryInfo;
import pl.us.ij.plugins.segmentation.infrastructure.CommandParams;
import pl.us.ij.plugins.segmentation.localization.Localization;

import javax.swing.*;
import java.util.ResourceBundle;

/**
 * Created by IntelliJ IDEA.
 * User: Przemek
 * Date: 2010-05-26
 * Time: 00:33:11
 * To change this template use File | Settings | File Templates.
 */
public class EdgesSkeletonizationForm extends CategoryInfo {
    private JButton skeletonButton;
    private JButton branchCuttingButton;
    private JPanel bonePanel;
    private JSpinner iterationCountSpinner;
    private JCheckBox autoCheckBox;

    public EdgesSkeletonizationForm(IActionHandler actionHandler) {
        actionHandler.setAction(skeletonButton, SkeletonCommand.class, getSkeletonParams());
        actionHandler.setAction(branchCuttingButton, BranchCuttingCommand.class, getSkeletonParams());

        iterationCountSpinner.setModel(new SpinnerNumberModel(1, 1, 1000, 1));
    }

    private ICommandParamGather getSkeletonParams() {
        return new ICommandParamGather() {

            @Override
            public CommandParams getCommandParams() {
                SkeletonCommandParams params = new SkeletonCommandParams();

                params.setAuto(autoCheckBox.isSelected());
                params.setIterationCount(((Number) iterationCountSpinner.getValue()).intValue());

                return params;
            }
        };
    }

    @Override
    public String getTitle() {
        return Localization.getString("edgesSBCForm.title");
    }

    @Override
    public JPanel getPanel() {
        return bonePanel;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String getDescription() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new FormLayout("fill:p:noGrow", "center:d:noGrow"));
        bonePanel = new JPanel();
        bonePanel.setLayout(new FormLayout("fill:p:noGrow", "center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        CellConstraints cc = new CellConstraints();
        panel1.add(bonePanel, cc.xy(1, 1));
        skeletonButton = new JButton();
        this.$$$loadButtonText$$$(skeletonButton, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("edgesSBCForm.skeletonizationLabel"));
        bonePanel.add(skeletonButton, cc.xy(1, 9));
        branchCuttingButton = new JButton();
        this.$$$loadButtonText$$$(branchCuttingButton, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("edgesSBCForm.branchCuttingLabel"));
        bonePanel.add(branchCuttingButton, cc.xy(1, 11));
        final JLabel label1 = new JLabel();
        this.$$$loadLabelText$$$(label1, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("filterAveragedForm.iterationCountLabel"));
        bonePanel.add(label1, cc.xy(1, 1));
        iterationCountSpinner = new JSpinner();
        bonePanel.add(iterationCountSpinner, cc.xy(1, 3, CellConstraints.FILL, CellConstraints.DEFAULT));
        autoCheckBox = new JCheckBox();
        this.$$$loadButtonText$$$(autoCheckBox, ResourceBundle.getBundle("pl/us/ij/plugins/segmentation/localization/Localization").getString("edgesSBCForm.auto"));
        bonePanel.add(autoCheckBox, cc.xy(1, 5));
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadLabelText$$$(JLabel component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setDisplayedMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }

    /**
     * @noinspection ALL
     */
    private void $$$loadButtonText$$$(AbstractButton component, String text) {
        StringBuffer result = new StringBuffer();
        boolean haveMnemonic = false;
        char mnemonic = '\0';
        int mnemonicIndex = -1;
        for (int i = 0; i < text.length(); i++) {
            if (text.charAt(i) == '&') {
                i++;
                if (i == text.length()) break;
                if (!haveMnemonic && text.charAt(i) != '&') {
                    haveMnemonic = true;
                    mnemonic = text.charAt(i);
                    mnemonicIndex = result.length();
                }
            }
            result.append(text.charAt(i));
        }
        component.setText(result.toString());
        if (haveMnemonic) {
            component.setMnemonic(mnemonic);
            component.setDisplayedMnemonicIndex(mnemonicIndex);
        }
    }
}
